package com.briskemen.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * 服务端还有一个地方需要注意，因为服务端的Service需要被客户端来远程绑定，所以客户端要能够找到这个Service，
 * 可以通过先指定包名，之后再配置Action值或者直接指定Service类名的方式来绑定Service
 * 本例子采用配置 Action 的方案
 */
public class AidlService extends Service {
    private List<Book> mBookList;
    
    public AidlService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mBookList = new ArrayList<>();
        initData();
    }

    /**
     * 初始化数据
     */
    private void initData() {
        Book book1 = new Book("活着");
        Book book2 = new Book("或者");
        Book book3 = new Book("叶应是叶");
        Book book4 = new Book("https://github.com/leavesC");
        Book book5 = new Book("http://www.jianshu.com/u/9df45b87cfdf");
        Book book6 = new Book("http://blog.csdn.net/new_one_object");
        mBookList.add(book1);
        mBookList.add(book2);
        mBookList.add(book3);
        mBookList.add(book4);
        mBookList.add(book5);
        mBookList.add(book6);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return mStub;
    }

    private BookController.Stub mStub = new BookController.Stub() {
        @Override
        public List<Book> getBookList() throws RemoteException {
            return mBookList;
        }

        @Override
        public void addBookInOut(Book book) throws RemoteException {
            if (book != null){
                book.setName("服务器改了新书的名字 inout");
                mBookList.add(book);
            }else {
                Log.e("AidlService","接收到了一个空对象 inout");
            }
        }
    };
}
